/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    class Delayer {
        constructor(defaultDelay) {
            this.defaultDelay = defaultDelay;
            this.timeout = null;
            this.completionPromise = null;
            this.onSuccess = null;
            this.task = null;
        }
        trigger(task, delay = this.defaultDelay) {
            this.task = task;
            if (delay >= 0) {
                this.cancelTimeout();
            }
            if (!this.completionPromise) {
                this.completionPromise = new Promise((resolve) => {
                    this.onSuccess = resolve;
                }).then(() => {
                    this.completionPromise = null;
                    this.onSuccess = null;
                    var result = this.task && this.task();
                    this.task = null;
                    return result;
                });
            }
            if (delay >= 0 || this.timeout === null) {
                this.timeout = setTimeout(() => {
                    this.timeout = null;
                    if (this.onSuccess) {
                        this.onSuccess(undefined);
                    }
                }, delay >= 0 ? delay : this.defaultDelay);
            }
            return this.completionPromise;
        }
        cancelTimeout() {
            if (this.timeout !== null) {
                clearTimeout(this.timeout);
                this.timeout = null;
            }
        }
    }
    exports.Delayer = Delayer;
});
